#!/bin/sh
# $Id: portshaker.sh.in 151 2008-10-20 10:25:33Z romain.tartiere $

. @@SHAREDIR@@/portshaker/portshaker.subr

# usage
#	Display command line arguments for portshaker.
#
usage()
{
	echo "usage: $0 [-stv] [ -U | -u ports_tree ] [ -M | -m ports_tree ]" 1>&2
	exit 1
}

# source_ports_trees
#	Get a list of all available source ports trees.
#
source_ports_trees()
{
	(cd ${config_dir}/portshaker.d && ls -I)
}

# Entry point

args=`getopt Mm:stUu:v $*`
if [ $? -ne 0 ]; then
	usage
fi

_do_update=0
_do_merge=0
_debug_level=0
set -- $args
for i; do
	case "$i" in
		-s)
			echo $(source_ports_trees)
			exit 0
			;;
		-t)
			echo ${ports_trees}
			exit 0
			;;
		-v)
			_debug_level="$(($_debug_level+1))"
			shift
			;;
		-U)	_do_update=1
			_update_ports_trees="$(source_ports_trees)"
			shift
			;;
		-u)	_do_update=1
			_update_ports_trees="${_update_ports_trees} $2"; shift
			shift
			;;
		-M)	_do_merge=1
			_merge_ports_trees="${ports_trees}"
			shift
			;;
		-m)	_do_merge=1
			_merge_ports_trees="${_merge_ports_trees} $2"; shift
			shift
			;;
		--)	shift
			break
			;;
	esac
done

if [ $# -gt 0 ]; then
	usage
fi

if [ -z "${_update_ports_trees}${_merge_ports_trees}" ]; then
	# Assume -UM when no option is provided
	_do_update=1
	_update_ports_trees="$(source_ports_trees)"
	_do_merge=1
	_merge_ports_trees="${ports_trees}"
fi

if [ ${_debug_level} -gt 0 ]; then
	export portshaker_info="YES"
	if [ ${_debug_level} -gt 1 ]; then
		export portshaker_debug="YES"
	fi
fi

if [ "${_do_update}" -eq 1 ]; then
	for _ports_tree in ${_update_ports_trees}; do
		${config_dir}/portshaker.d/${_ports_tree} update
		if [ $? -ne 0 ]; then
			err 1 "Failed to update the '${_ports_tree}' ports tree."
		fi
	done
fi

if [ "${_do_merge}" -eq 1 ]; then
	for _ports_tree in ${_merge_ports_trees}; do
		_target=`eval echo \\\$${_ports_tree}_ports_tree`
		_merge_from=`eval echo \\\$${_ports_tree}_merge_from`
		_first=1
		for _source in ${_merge_from}; do
			_merge_flags=""
			if [ "${_first}" -ne 1 -a "x${_source#${_source%?}}" = 'x!' ]; then
				# Auto-install modified ports.
				_source=${_source%?}
				_merge_flags="-a"
			fi
			if [ "${_first}" -eq 1 ]; then
				${config_dir}/portshaker.d/${_source} clone_to -t ${_target} || exit 1
				_first=0
			else
				${config_dir}/portshaker.d/${_source} merge_to ${_merge_flags} -t ${_target} || exit 1
			fi
		done
	done
fi

exit 0
